package com.zx.map.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.zx.exception.GlobalServiceException;
import com.zx.map.dao.ConsumeLogDao;
import com.zx.map.dao.model.ConsumeLog;
import com.zx.map.dao.model.ConsumeLogExample;
import com.zx.map.service.BaseAcctInfoService;
import com.zx.map.service.ConsumeLogService;
import com.zx.map.service.CustAcctLogService;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.UserCountService;
import com.zx.map.service.model.BaseAcctInfoModel;
import com.zx.map.service.model.ConsumeLogModel;
import com.zx.map.service.model.CustAcctLogModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.mobile.service.proxy.ConsumeCommonProxy;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("consumeLogService")
public class ConsumeLogServiceImpl implements ConsumeLogService {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(ConsumeLogServiceImpl.class);

    @Autowired
    private ConsumeLogDao dao;
    @Autowired
    private BaseAcctInfoService baseAcctInfoService;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private CustAcctLogService custAcctLogService;
    @Autowired
    private MerchantInfoService merchantInfoService;
 


    /**
     * 消费处理
     */
    public void dealConsumeLog(final ConsumeLogModel consumeLogModel, final String merchantId) {
        log.info("消费充值，开始处理");
        transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus arg0) {
                //增加行锁读
                BaseAcctInfoModel temp = baseAcctInfoService
                        .getModelByKeyAndLineLock(merchantId);
                if (null != temp) {
                    //检查是否重复订单
                    ConsumeLogModel params = new ConsumeLogModel();
                    params.setAccessOrdId(consumeLogModel.getAccessOrdId());
                    List<ConsumeLogModel> existList = getListByModel(params);
                    if (null != existList && existList.size() >= 1) {
                        log.info("重复订单异常," + consumeLogModel.getAccessOrdId());
                        throw new GlobalServiceException("平安充值出现重复订单 不做任何处理");
                    } else {
                        if (temp.getAvlBal() >= consumeLogModel.getConsumeAmt()) {//可用余额足够即可充值
                            // 0、消费充值账户余额减少
                            temp = ConsumeCommonProxy.transformBaseAcct(temp,
                                    consumeLogModel.getAccessTradeAmt().toString());
                            CustAcctLogModel custModel = ConsumeCommonProxy.transformCustAcct(temp, consumeLogModel);
                            custAcctLogService.add(custModel);
                            baseAcctInfoService.update(temp);
                            MerchantInfoModel currentModel = merchantInfoService.getModelByKey(consumeLogModel.getMerchantId());
                            consumeLogModel.setMerSysAccount(currentModel.getSysAccount());
                            consumeLogModel.setAgentId(currentModel.getAgentId());
                            consumeLogModel.setAgentSysAccount(currentModel.getAgentSysAccount());
                            //1、增加消费充值记录
                            add(consumeLogModel);
                            
                         
                            
                        } else {
                            throw new GlobalServiceException("0002", "账户余额不足");
                        }
                    }
                } else {
                    throw new GlobalServiceException("0004", "用户不存在");
                }
                return null;
            }
        });
        log.info("消费充值，处理结束");
    }

    /**
     * 根据Model查找列表
     */
    public List<ConsumeLogModel> getListByModel(ConsumeLogModel model)
            throws DataAccessException {
        ConsumeLogExample example = new ConsumeLogExample();
        ConsumeLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(model.getMerchantId())) {
            criteria.andMerchantIdEqualTo(model.getMerchantId());
        }
        //增加订单号检索
        if (StringUtils.isNotBlank(model.getAccessOrdId())) {
            criteria.andAccessOrdIdEqualTo(model.getAccessOrdId());
        }

        List<ConsumeLog> list = dao.getListByExample(example);
        List<ConsumeLogModel> modelList = new ArrayList<ConsumeLogModel>();
        for (ConsumeLog o : list) {
            ConsumeLogModel temp = new ConsumeLogModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

    /**
     * 新增
     */
    public String add(ConsumeLogModel model) throws DataAccessException {
        ConsumeLog temp = new ConsumeLog();
        BeanUtils.copyProperties(model, temp);
        String key = dao.add(temp);
        return key;
    }

    /**
     * 修改
     */
    public String update(ConsumeLogModel model) throws DataAccessException {
        ConsumeLog temp = new ConsumeLog();
        BeanUtils.copyProperties(model, temp);
        return dao.update(temp);
    }

    /**
     * 删除
     */
    public void delete(ConsumeLogModel model) throws DataAccessException {
        ConsumeLog temp = new ConsumeLog();
        BeanUtils.copyProperties(model, temp);
        dao.delete(temp);
    }


}
